package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "微博模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation(value = "发布微博功能")
    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO,@ApiIgnore HttpSession session){
        System.out.println("开始发表微博...");
        System.out.println(weiboDTO);
        //验证登录
        User user = (User) session.getAttribute("user");
        if(user==null){//没登陆
            return 2;
        }
        //将微博信息插入
        Weibo weibo = new Weibo();
        weibo.setContent(weiboDTO.getContent());
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }

    @ApiOperation(value = "微博首页列表")
    @GetMapping("/selectIndex")
    public List<WeiboIndexVO> findIndexWeibo(){
        return weiboMapper.selectAll();
    }

    @ApiOperation(value = "微博详情功能")
    @GetMapping("/selectById")
    @ApiImplicitParam(value = "微博Id",name = "id",required = true,example = "10",dataType = "int")
    public WeiboDetailVO findById(Integer id){
        return weiboMapper.selectById(id);
    }
}
